package 锁.lock.Condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/9/5 15:07
 * @description:
 * @version:
 */
public class waitNotify {
    //可重入锁
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public void await(){
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "准备进行等待");
            Thread.sleep(3000);
            //这里等待以后，其他线程就有了获取该锁的机会了，当其他线程将该锁唤醒以后，就再次继续执行
            condition.await();
            System.out.println(Thread.currentThread().getName() + "结束等待");
            lock.unlock();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void signalAll(){
        System.out.println("kkkkkkkkkk");
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "准备通知");
        condition.signal();
        System.out.println(Thread.currentThread().getName() +  "通知完后在再释放锁");
        System.out.println(Thread.currentThread().getName() + "通知结束");
        lock.unlock();
    }
}


